<?php
/*
Plugin Name: JayBo Category Widget
Plugin URI: http://www.jaybowalkin.com/categories/plugins
Description: Widget is exactly like the current categories widget, however it allows for excluding certain categories
ategories
Author: Bryan Clark
Version: 0.1
Author URI: http://www.jaybowalkin.com/

*/ 

function jaybo_get_category_args() {
	$options = get_option('jaybo_widget_categories');
	$c = $options['count'] ? '1' : '0';
	$h = $options['hierarchical'] ? '1' : '0';
	$exclude = empty( $options['exclude'] ) ? '' : '&exclude=' . $options['exclude'];

	return "orderby=term_order&show_count={$c}&hierarchical={$h}" . $exclude;
}

function jaybo_widget_categories($args) {
	extract($args);
	$options = get_option('jaybo_widget_categories');
	$c = $options['count'] ? '1' : '0';
	$h = $options['hierarchical'] ? '1' : '0';
	$d = $options['dropdown'] ? '1' : '0';
	$title = empty($options['title']) ? __('Categories') : $options['title'];
	$exclude = empty( $options['exclude'] ) ? '' : '&exclude=' . $options['exclude'];

	echo $before_widget;
	echo $before_title . $title . $after_title; 

	$cat_args = jaybo_get_category_args();

	if($d) {
		wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category'));
?>

<script lang='javascript'><!--
    var dropdown = document.getElementById("cat");
    function onCatChange() {
		if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
	        location.href = "<?php echo get_option('siteurl'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
		}
    }
    dropdown.onchange = onCatChange;
--></script>

<?php
	} else {
?>
		<div class="sidebar_section">
		<ul>
		<?php wp_list_categories($cat_args . '&title_li='); ?>
		</ul>
		</div>
<?php
	}

	echo $after_widget;
}

function jaybo_widget_categories_control() {
	$options = $newoptions = get_option('jaybo_widget_categories');
	if ( $_POST['jaybo-categories-submit'] ) {
		$newoptions['count'] = isset($_POST['jaybo-categories-count']);
		$newoptions['hierarchical'] = isset($_POST['jaybo-categories-hierarchical']);
		$newoptions['dropdown'] = isset($_POST['jaybo-categories-dropdown']);
		$newoptions['title'] = strip_tags(stripslashes($_POST['jaybo-categories-title']));
		$newoptions['exclude'] = strip_tags( stripslashes( $_POST['jaybo-categories-exclude'] ) );
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('jaybo_widget_categories', $options);
	}
	$count = $options['count'] ? 'checked="checked"' : '';
	$hierarchical = $options['hierarchical'] ? 'checked="checked"' : '';
	$dropdown = $options['dropdown'] ? 'checked="checked"' : '';
	$title = attribute_escape($options['title']);
	$exclude = attribute_escape( $options['exclude'] );
?>
			<p><label for="jaybo-categories-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="jaybo-categories-title" name="jaybo-categories-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="jaybo-categories-count"><?php _e('Show post counts'); ?> <input class="checkbox" type="checkbox" <?php echo $count; ?> id="jaybo-categories-count" name="jaybo-categories-count" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="jaybo-categories-hierarchical" style="text-align:right;"><?php _e('Show hierarchy'); ?> <input class="checkbox" type="checkbox" <?php echo $hierarchical; ?> id="jaybo-categories-hierarchical" name="jaybo-categories-hierarchical" /></label></p>
			<p style="text-align:right;margin-right:40px;"><label for="jaybo-categories-dropdown" style="text-align:right;"><?php _e('Display as a drop down'); ?> <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="jaybo-categories-dropdown" name="jaybo-categories-dropdown" /></label></p>
			<p><label for="jaybo-categories-exclude"><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $exclude; ?>" name="jaybo-categories-exclude" id="jaybo-categories-exclude" style="width: 180px;" /></label><br />
			<small><?php _e( 'Page IDs, separated by commas.' ); ?></small></p>
			<input type="hidden" id="jaybo-categories-submit" name="jaybo-categories-submit" value="1" />
<?php
}

function jaybo_category_widgets_init() {
	global $wp_register_widget_defaults;

	$wp_register_widget_defaults = true;
	$dims200 = array('height' => 200, 'width' => 300);
	$class = array('classname' => 'widget_categories');
	wp_register_sidebar_widget('jaybo_categories', __('JayBo Categories'), 'jaybo_widget_categories', $class);
	wp_register_widget_control('jaybo_categories', __('JayBo Categories'), 'jaybo_widget_categories_control', $dims150);

	$wp_register_widget_defaults = false;

	do_action('widgets_init');
}

add_action('init', 'jaybo_category_widgets_init', 1);

